﻿using Jupiter.Infrastructure;
using Jupiter.Infrastructure.Cache.Interfaces;
using Jupiter.Infrastructure.Domain;
using Jupiter.Infrastructure.Domain.SqlSugar.Basic;
using Jupiter.Infrastructure.Permission;
using Jupiter.Infrastructure.Permission.DataPermissions;
using MediatR;
using Microsoft.Extensions.Configuration;
using Wips.AppService.Publisher.AgentServerPermissions.Requests;
using Wips.Domain.Models;

namespace Wips.AppService.SqlSugar.Publisher
{

    /// <summary>
    /// 服务器授权操作
    /// </summary>

    public class AgentServerPermissionRequestHandler : RequestHandlerBase<AgentServer>,
        IRequestHandler<SaveAgentPermissionRequest, string>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="securityContextAccessor"></param>
        /// <param name="facadeResolver"></param>
        /// <param name="cacheManager"></param>
        /// <param name="configuration"></param>
        /// <param name="dataPermission"></param>
        public AgentServerPermissionRequestHandler(ISqlSugarClient dbContext, ISecurityContextAccessor securityContextAccessor, IFacadeResolver facadeResolver, ICacheManager cacheManager, IConfiguration configuration, IDataPermission? dataPermission) : base(dbContext, securityContextAccessor, facadeResolver, cacheManager, configuration, dataPermission)
        {
        }

        /// <summary>
        /// 保存授权
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<string> Handle(SaveAgentPermissionRequest request, CancellationToken cancellationToken)
        {
            RegisterDeleteObjectValueRange<AgentServerPermission>(c => c.UserId == request.UserId);
            List<AgentServerPermission> addList = new List<AgentServerPermission>();
            if (request.AgentServerIds != null && request.AgentServerIds.Count > 0)
            {

                foreach (var item in request.AgentServerIds)
                {
                    var entity = new AgentServerPermission
                    {
                        Id = ObjectId.GenerateNewStringId(),
                        UserId = request.UserId,
                        AgentServerId = item,
                        AgentServerConfigId = null
                    };
                    addList.Add(entity);
                }
            }
            if (request.AgentServerConfigIds != null && request.AgentServerConfigIds.Count > 0)
            {
                foreach (var item in request.AgentServerConfigIds)
                {
                    var entity = new AgentServerPermission
                    {
                        Id = ObjectId.GenerateNewStringId(),
                        UserId = request.UserId,
                        AgentServerId = null,
                        AgentServerConfigId = item
                    };
                    addList.Add(entity);
                }
            }

            if (addList.Count > 0)
            {
                await RegisterNewObjectValueRangeAsync(addList, cancellationToken: cancellationToken);
            }

            return "";
        }


    }
}
